package com.changpay.zywy.ui.activity.loan

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.MoneyInfoAdapter
import com.changpay.zywy.api.WebApis
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.HuoKuanBean
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.web.WebViewActivity
import com.changpay.zywy.utils.DialogUtil
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_finish_account.*
import kotlinx.android.synthetic.main.title_layout.*

class FinishAccountActivity : BaseActivity(), IView {
    private var pay_money: Double = 0.0
    private var pay_select: Boolean = false
    private var settle_type: String = "1"
    private var code: String = ""
    private var tempData: HuoKuanBean? = null

    var settle_type_text: String = ""
    private val mDialog by lazy {
        DialogUtil.getWaitDialog(this,"")
    }
    override fun showLoading() {
        mDialog.show()
    }

    override fun hideLoading() {
        mDialog.dismiss()
    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
        content.visibility = View.GONE
        emty_view.visibility = View.VISIBLE

    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.btn_submit -> submit()
            R.id.rv_weijie_mingxi -> Intent(this, FinishAccountDetailActivity::class.java).putExtra(
                "allot_id",
                intent.getStringExtra("allot_id")
            ).run {
                startActivity(this)
            }
            R.id.rv_history -> Intent(this, LoanHistoryActivity::class.java).putExtra(
                "allot_id",
                intent.getStringExtra("allot_id")
            ).run {
                startActivity(this)
            }
        }
    }

    private fun submit() {
        if (pay_select) {  // 是否有选中接中金额
            if (pay_money == 0.0) {
                showToast("本期结算或收款已完成")
            } else {
                if (settle_type == "1") {   // 验证时否勾选了同意按钮
                    if (cb_tongyi.isChecked) {
                        Intent(this, LoanPayActivity::class.java).run {
                            putExtra("jiesuan_message", tempData?.settle_type_text)
                            putExtra("money", pay_money.toString())
                            putExtra("code", code)
                            putExtra("allot_id", intent.getStringExtra("allot_id"))
                            putExtra("type", "结账")
                            startActivity(this)
                        }
                    } else {
                        showToast("请先同意合作协议书")
                    }
                } else {
                    Intent(this, LoanPayActivity::class.java).run {
                        putExtra("allot_id", intent.getStringExtra("allot_id"))
                        putExtra("jiesuan_message", tempData?.settle_type_text)
                        putExtra("money", pay_money.toString())
                        putExtra("code", code)
                        putExtra("type", "收帐")
                        startActivity(this)
                    }
                }
            }
        } else {
            if (settle_type == "1") {
                showToast("请先选择结算金额")
            } else {
                showToast("请先选择收款金额")
            }

        }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_finish_account
    }

    override fun initData() {
    }

    override fun initView() {

        tv_title.text = "查账结算"
        iv_back.setOnClickListener(onClickListener)
        tv_xieyi.setOnClickListener {
            WebViewActivity.startActivity(this, WebApis.cooperationAgreement)
        }
        rv_weijie_mingxi.setOnClickListener(onClickListener)
        btn_submit.setOnClickListener(onClickListener)
        rv_history.setOnClickListener(onClickListener)
        initRecyclerView()

    }

    override fun start() {

        val param = hashMapOf<String, String>()
        param["key"] = token
        param["allot_id"] = intent.getStringExtra("allot_id")
        RetrofitHelper.service.get_tBill_detail(param).sss(this)
        {
            tempData = it.result
            settle_type = tempData!!.settle_type
            if (tempData!!.settle_type == "1") {
                tv_xieyi.visibility = View.VISIBLE
                ll_red_title.visibility = View.GONE
                cb_tongyi.visibility = View.VISIBLE
                tv_type_title.text = "请选择您的结款金额"
                btn_submit.text = "直接结款"
                tv_fen_qi.text = "分期后可按分期结款，也可选择结算余款一次性结清"
                tv_rizhi_type.text = "本期最后结款日为"
            } else {
                tv_xieyi.visibility = View.GONE
                ll_red_title.visibility = View.GONE
                cb_tongyi.visibility = View.GONE
                tv_type_title.text = "请选择您的收款金额"
                btn_submit.text = "确认收款"
                tv_fen_qi.text = "分期后可按分期收款，也可选择结算余款一次性收款"
                tv_rizhi_type.text = "本期最后收款日为"

            }
            settle_type_text = tempData!!.settle_type_text

            datas.addAll(tempData!!.refund_amount)
            mAdapter.notifyDataSetChanged()

            tv_weijie_money.text = "未结金额：￥" + tempData!!.settle_info.still_total
            tv_benqi_money.text = "本期需结金额：￥" + tempData!!.settle_info.current
            tv_yijie_money.text = "已结金额：￥" + tempData!!.settle_info.also
            tv_finish_time.text = tempData!!.settle_info.refund_date


            mess_one.text = "划拨产品：" + tempData!!.allot_info.goods_name
            mess_two.text = "划拨人：" + tempData!!.allot_info.p_member_name
            mess_three.text = "接收人：" + tempData!!.allot_info.member_name
            mess_four.text = "划拨台数：" + tempData!!.allot_info.num + "台"
            mess_five.text = "机具单价：￥" + tempData!!.allot_info.unit_price
            mess_six.text = "机具总价：￥" + tempData!!.allot_info.amount


        }
    }


    /**
     * datas
     */
    private val datas = mutableListOf<HuoKuanBean.RefundAmountBean>()


    /**
     * Adapter
     */
    private val mAdapter: MoneyInfoAdapter by lazy {
        MoneyInfoAdapter(App.context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(App.context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        datas.forEach {
            it.isSelect = false
        }
        datas[position].isSelect = true
        pay_select = true
        pay_money = datas[position].amount.toDouble()
        code = datas[position].code
        mAdapter.notifyDataSetChanged()
    }

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(App.context!!)
        }
    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        when (view.id) {
        }
    }

    private fun initRecyclerView() {
        recycleView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recycleView)
            onItemChildClickListener = this@FinishAccountActivity.onItemChildClickListener

//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@FinishAccountActivity.ItemClickListener
        }
    }
}
